
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser'); //cookie模块
var logger = require('morgan');  //日志模块

// 引入路由
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// localhost:3000   http://127.0.0.1:5500     http://127.0.0.8:8888
// 解决 CORS 跨域 后端解决跨域
app.use('*', function (req, res, next) {
  // res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:8020');
  res.setHeader('Access-Control-Allow-Origin', '*') //允许所有请求地址
  res.setHeader('Access-Control-Allow-Methods', '*') //允许所有的请求方式
  res.setHeader('Access-Control-Allow-Headers', 'content-type') //允许所有的请求头类型
  next()
})

app.use(logger('dev')); //配置日志模块
// 解决post请求解析问题
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use(cookieParser()); //配置cookie模块

// 配置静态资源路径
app.use(express.static(path.join(__dirname, 'public')));

//  引入路由
app.use('/', indexRouter);
app.use('/users', usersRouter);



module.exports = app;
